﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace ProjetIntegrationMobilite
{
    public class Board
    {
        private DateTime _releaseDate;
        private string _pseudoUser1;
        private string _pseudoUser2;
        private string _size;
        private string _name;
        public string status;
        private bool _color;
        private bool white_black = false;
        public string url;
        public List<Moves> listMove = new List<Moves>();
        PointsJeu[] petitPoint;
        private int k = 0;

        public Board()
        {
        }

        public Board(string pseudoUser1, string pseudoUser2, string name, string size, DateTime releaseDate,bool color)
        {
            owner = pseudoUser1;
            invite = pseudoUser2;
            creation_date = releaseDate;
            Size = size;
            Name = name;
            owner_white = color;
            name = owner + "vs" + invite + releaseDate;
            url = "http://api.goimerir.com/boards/" + Name;
            CréationPlateauDeJeu();
        }

        #region get and set of board information
        public DateTime creation_date
        {
            get
            {
                if (_releaseDate == null)
                    throw new Exception("ReleaseDate improperly initialized");
                return _releaseDate;
            }

            set
            {
                if (value == null
                    || value.CompareTo(new DateTime(2011, 10, 1)) < 0
                    || value.CompareTo(DateTime.Now.AddYears(1)) >= 0)
                    throw new ArgumentException("Board cannot be null or empty");
                _releaseDate = value;
            }
        }

        public string owner
        {
            get
            {
                if (_pseudoUser1.Length == 0)
                    throw new Exception("NomUser1 improperly initialized");
                return _pseudoUser1;
            }
            set
            {
                if (value == null || value.Length == 0)
                    throw new ArgumentException("NomUser1 cannot be null or empty");
                _pseudoUser1 = value;
            }
        }

        public string invite
        {
            get
            {
                return _pseudoUser2;
            }
            set
            {
                _pseudoUser2 = value;
            }
        }

        public string Name
        {
            get
            {
                if (_name.Length == 0)
                    throw new Exception("ID improperly initialized");
                return _name;
            }
            set
            {
                if (value == null || value.Length == 0)
                    throw new ArgumentException("ID cannot be null or empty");
                _name = value;
            }
        }

        public string Size
        {
            get
            {
                return _size;
            }
            set
            {
               // if (value == null || value.Length == 0)
                    //throw new ArgumentException("NomUser2 cannot be null or empty");
                _size = "9x9";
            }
        }

        public bool owner_white
        {
            get
            {
                if (_size.Length == 0)
                    throw new Exception("NomUser2 improperly initialized");
                return _color;
            }
            set
            {
                if (value == null)
                    throw new ArgumentException("NomUser2 cannot be null or empty");
                _color = true;
            }
        }
        #endregion

        private void CréationPlateauDeJeu()
        {
            petitPoint = new PointsJeu[81];
            this.k = 0;
            #region Creating the game board
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    petitPoint[k] = new PointsJeu(i, j, -1);
                    k++;
                }
            }
            #endregion
        }

        public int PostionPoints(int X, int Y)
        {
            #region Test if the player misses a turn or an invalid field
            if (X == null && Y == null)
            {
                return 1;
            }
            else if (X == null || Y == null)
            {
                return 0;
            }
            #endregion

            #region Positioning of the point
            else if (X != null && Y != null)
            {
                for (int i = 0; i < 81; i++)
                {
                    if (petitPoint[i].coordX == X && petitPoint[i].coordY == Y && petitPoint[i].choixPoint == -1)
                    {
                        if (white_black == true)
                        {
                            petitPoint[i].choixPoint = 0;
                            white_black = false;
                        }
                        else
                        {
                            petitPoint[i].choixPoint = 1;
                            white_black = true;
                        }
                        return 1;
                    }
                }
            }
            return 0;
            #endregion
        }
    }
}
